//
//  fGGoodsHeaderView.m
//  fGeekApp
//
//  Created by Administrator on 15/3/10.
//  Copyright (c) 2015年 cwx. All rights reserved.
//

#import "fGGoodsHeaderView.h"
@interface fGGoodsHeaderView()<UIScrollViewDelegate>
/**商品图片*/
@property (weak, nonatomic) IBOutlet UIImageView *goodsIconView;
/**图片轮播器*/
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/**分页控制符*/
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

/** 定时器*/
@property NSTimer *timer;

@end

@implementation fGGoodsHeaderView

+ (instancetype)goodsHeaderView{
    return [[[NSBundle mainBundle] loadNibNamed:@"fGGoodsHeaderView" owner:nil options:nil] lastObject];
}

// 一创建xib的时候调用(醒来)， init方法用来用代码创建时候调用
-(void)awakeFromNib{
    
    // 去掉下拉框
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    // 设置page属性
    self.scrollView.pagingEnabled = YES;
    
    //设置图片数量 total
    int total = 5;
    
    //设置图片高度和宽度
    CGFloat imageHeight = self.goodsIconView.frame.size.height;
    
    CGFloat imageWidth = self.goodsIconView.frame.size.width;
    
    CGFloat imageY = 0;
    
    //设置滚动范围
    self.scrollView.contentSize = CGSizeMake(total * imageWidth, 0);
    
    // 加载图片(自己创建)
    for (int i = 0 ; i  < total; i++) {
        
        UIImageView *image = [[UIImageView alloc] init];
        
        image.image = [UIImage imageNamed:[[NSString alloc] initWithFormat:@"ad_0%d",i]];
        
        image.frame = CGRectMake(i *  imageWidth, imageY, imageWidth, imageHeight);
        
        [self.goodsIconView addSubview:image];
        
        
    }
    
    // 设置页码的总数量
    self.pageControl.numberOfPages = total;
    
    // 设置代理对象
    self.scrollView.delegate = self;
    
    [self addTimer];
}

#pragma mark 自定义的方法
/** 安装定时器 */
-(void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    //将定时器用在主线程中
    
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes ];
}

/**  移除定时器 */
-(void)removeTimer{
    [self.timer invalidate];
    self.timer = nil;
}

/** 下一张图片 */
-(void) nextImage {
    
    CGFloat imageWidth = self.goodsIconView.frame.size.width;
    
    
    //这里只是为了得到 page做事情，不跟下面判断第几张图片冲突
    int page = 0 ;
    
    if (self.pageControl.currentPage == 4) {
        page = 0 ;
    }
    else
    {
        page =  self.pageControl.currentPage + 1;
    }
    
    CGPoint point;
    
    point.x = page * imageWidth;
    
    point.y = 0;
    
    //设置一张滚动一张
    [self.scrollView setContentOffset:point animated:YES];
}

/** 在用户要开始拖的时候，停止定时器*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}

/** 在用户松手的时候，增加计时器 */
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}

#pragma  mark  scrollView的代理方法
/** 在拖动的时候，主要用来判断在第几张 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat imageWidth = self.goodsIconView.frame.size.width;
    
    int page = (self.scrollView.contentOffset.x + imageWidth * 0.5 ) / imageWidth;
    
    self.pageControl.currentPage = page;
    
}


@end
